// 饼图
// 基于准备好的dom，初始化echarts实例
var myChart = echarts.init(document.getElementById('dataCount'));

// 指定图表的配置项和数据
var option = {
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
        orient: 'vertical',
        left: 'left',
        right: 10,
        top: 20,
        bottom: 10,
        data: ['物业','车辆','车位']
    },
    series: [
        {
            name:'用户租车',
            type:'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
                normal: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    show: true,
                    textStyle: {
                        fontSize: '25',
                        fontWeight: 'bold'
                    }
                }
            },
            labelLine: {
                normal: {
                    show: false
                }
            },
            data:[
                {value:100, name:'物业'},
                {value:100, name:'车辆'},
                {value:104, name:'车位'}
        ]
        }
    ]
}

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

myChart.showLoading();


layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/platform/count",
        type: "get",
        success: function (result) {

            myChart.hideLoading();
            document.getElementById("userCount").innerHTML = result.userCount-1;


            // 填入数据
            myChart.setOption({

                series: [{
            data:[
                {value:result.comCount, name:'物业'},
                {value:result.carCount, name:'车辆'},
                {value:result.carParkCount, name:'车位'}

            ]
        }]
            });

        }
    });
});



var websocket = null;
//判断当前浏览器是否支持WebSocket
if ('websocket' in window) {
    var account=$("#account").val();

    websocket = new WebSocket("ws://localhost:80/sharepark/ws/onlineServer/"+account);

    //连接成功建立的回调方法
    websocket.onopen = function () {
        // alert("客户端连接成功");
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        // alert("客户端接收到消息");
        setMessageInnerHTML(event.data);
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        // alert("WebSocket连接发生错误");
    };

    //连接关闭的回调方法
    websocket.onclose = function () {
        // alert("WebSocket连接关闭");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    }

}
else {
    // alert('当前浏览器 Not support websocket')
}

//将消息显示在网页上
function setMessageInnerHTML(count) {

    // $("#count").innerHTML(count);
    document.getElementById('onlineCount').innerHTML = count;


}

//关闭WebSocket连接
function closeWebSocket() {
    websocket.close();
}

//统计平台分成
layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/platform/getMoney",
        type: "get",
        success: function (result) {
            document.getElementById("money").innerHTML = result;



        }
    });
});


//统计小区车位情况
layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/platform/getTotal",
        type: "get",
        success: function (result) {
            document.getElementById("unCheckParkCount").innerHTML = result.unCheckParkCount;
            document.getElementById("complaintCount").innerHTML = result.complaintCount;
            document.getElementById("orderCount").innerHTML = result.orderCount;
            document.getElementById("busyParkCount").innerHTML = result.busyParkCount;



        }
    });
});

var myChart1 = echarts.init(document.getElementById('map'));

myChart1.showLoading();


layui.use(('jquery'), function(){
    var $ = layui.$;

    // 统计后台数据
    $.ajax({
        url: "/sharepark/platform/getComCount",
        type: "get",
        success: function (result) {

            myChart1.hideLoading();

            myChart1.setOption({
                title : {
                    text: '小区分布(主要省份)',

                },
                tooltip : {
                    trigger: 'axis'
                },
                legend: {
                    data:['数量']
                },
                toolbox: {
                    show : true,
                    feature : {
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                xAxis : [
                    {
                        type : 'category',
                        data : ['北京','上海','广东','重庆','江苏','四川','山东','湖南','湖北','天津','福建','河北']
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : [

                    {
                        name:'数量',
                        type:'bar',
                        data:[result.countBJ, result.countSH, result.countGD,result.countCQ, result.countJS,
                            result.countSC,result.countSD,result.countHN,result.countHB, result.countTJ,result.countFJ,result.countHEB]

                    }
                ]
            });
        }
    });
});